home *** CD-ROM | disk | FTP | other *** search
- /**********************************************************************\
-
- File: ls endgame.c
-
- Purpose: This module handles drawing the endgame board.
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program in a file named "GNU General Public License".
- If not, write to the Free Software Foundation, 675 Mass Ave,
- Cambridge, MA 02139, USA.
-
- \**********************************************************************/
-
- #include "ls endgame.h"
- #include "ls find.h"
- #include "ls meat.h"
- #include "sounds.h"
- #include "graphics.h"
- #include "menus.h"
- #include "util.h"
- #include "V bars scroll thin fade.h"
- #include "program globals.h"
-
- static Boolean gShowingEndGame;
- static PicHandle gCongratsColorPict, gCongratsBWPict;
- static PicHandle gWinnerColorPict, gWinnerBWPict;
-
- enum
- {
- congratsColorID=400,
- congratsBWID,
- winnerColorID,
- winnerBWID
- };
-
- void DrawCenteredString(Str255 theStr, short *theY, short width);
- void AddToString(Str255 theStr, Str255 addStr);
- short BestSolution(short numRows, short numColumns);
- short FindNumUsed(void);
-
- Boolean GameOverQQ(void)
- {
- short theRow, theColumn;
-
- for (theRow=0; theRow<gNumRows; theRow++)
- {
- for (theColumn=0; theColumn<gNumColumns; theColumn++)
- {
- if (Board[theRow][theColumn]==0)
- return FALSE;
- }
- }
-
- return !FindProblem(&theRow, &theColumn, &theRow, &theColumn);
- }
-
- void CheckEndGame(WindowDataHandle theData)
- {
- if (GameOverQQ())
- {
- VerticalBarsScrollThinFade(GetWindowGrafPtr(theData)->portRect, &black);
- gShowingEndGame=TRUE;
- AdjustMenus();
- DrawMenuBar();
- DoSound(sound_endgame, TRUE);
- (**theData).offscreenNeedsUpdate=TRUE;
- UpdateTheWindow((ExtendedWindowDataHandle)theData);
- }
- }
-
- Boolean ShowingEndGameQQ(void)
- {
- return gShowingEndGame;
- }
-
- void DontShowEndGame(WindowDataHandle theData)
- {
- gShowingEndGame=FALSE;
- AdjustMenus();
- DrawMenuBar();
- (**theData).offscreenNeedsUpdate=TRUE;
- UpdateTheWindow((ExtendedWindowDataHandle)theData);
- }
-
- void InitTheEndGame(void)
- {
- gCongratsColorPict=gCongratsBWPict=gWinnerColorPict=gWinnerBWPict=0L;
- gShowingEndGame=FALSE;
- }
-
- void ShutDownTheEndGame(void)
- {
- gCongratsColorPict=ReleaseThePict(gCongratsColorPict);
- gCongratsBWPict=ReleaseThePict(gCongratsBWPict);
- gWinnerColorPict=ReleaseThePict(gWinnerColorPict);
- gWinnerBWPict=ReleaseThePict(gWinnerBWPict);
- }
-
- void DrawEndGame(WindowDataHandle theData, short theDepth)
- {
- GrafPtr curPort;
- short theX, theY;
- Boolean isColor;
- short best;
- short numUsed;
- short width;
- Str255 theStr, numStr;
-
- isColor=(theDepth>2);
- GetPort(&curPort);
- FillRect(&(curPort->portRect), black);
- width=curPort->portRect.right-curPort->portRect.left;
- theX=(**theData).windowWidth/2;
- theY=(curPort->portRect.bottom-curPort->portRect.top)/3;
- best=BestSolution(gNumRows, gNumColumns);
- numUsed=FindNumUsed();
- if (numUsed==best)
- {
- if (isColor)
- gWinnerColorPict=DrawThePicture(gWinnerColorPict, winnerColorID, theX, theY, TRUE);
- else
- gWinnerBWPict=DrawThePicture(gWinnerBWPict, winnerBWID, theX, theY, TRUE);
-
- theStr[0]=0x00;
- AddToString(theStr, "\pYou’ve completed the ");
- NumToString(gNumRows, numStr);
- AddToString(theStr, numStr);
- AddToString(theStr, "\p x ");
- NumToString(gNumColumns, numStr);
- AddToString(theStr, numStr);
- AddToString(theStr, "\p board");
- theY=(curPort->portRect.bottom-curPort->portRect.top)/2;
- DrawCenteredString(theStr, &theY, width);
- theStr[0]=0x00;
- AddToString(theStr, "\pwith the minimum number of pieces!");
- DrawCenteredString(theStr, &theY, width);
- theStr[0]=0x00;
- DrawCenteredString(theStr, &theY, width);
- if (gNumRows<MAX_ROWS)
- {
- AddToString(theStr, "\p(Time to try the ");
- NumToString(gNumRows+1, numStr);
- AddToString(theStr, numStr);
- AddToString(theStr, "\p x ");
- NumToString(gNumColumns+1, numStr);
- AddToString(theStr, numStr);
- AddToString(theStr, "\p board, no?)");
- DrawCenteredString(theStr, &theY, width);
- }
- else
- {
- AddToString(theStr, "\p(If you’ve already completed the other");
- DrawCenteredString(theStr, &theY, width);
- theStr[0]=0x00;
- AddToString(theStr, "\plevels, you’re better than the author!)");
- DrawCenteredString(theStr, &theY, width);
- }
- }
- else
- {
- if (isColor)
- gCongratsColorPict=DrawThePicture(gCongratsColorPict, congratsColorID, theX, theY, TRUE);
- else
- gCongratsBWPict=DrawThePicture(gCongratsBWPict, congratsBWID, theX, theY, TRUE);
-
- theStr[0]=0x00;
- AddToString(theStr, "\pYou’ve completed the ");
- NumToString(gNumRows, numStr);
- AddToString(theStr, numStr);
- AddToString(theStr, "\p x ");
- NumToString(gNumColumns, numStr);
- AddToString(theStr, numStr);
- AddToString(theStr, "\p board");
- theY=(curPort->portRect.bottom-curPort->portRect.top)/2;
- DrawCenteredString(theStr, &theY, width);
- theStr[0]=0x00;
- AddToString(theStr, "\pwith ");
- NumToString(numUsed, numStr);
- AddToString(theStr, numStr);
- AddToString(theStr, "\p unique pieces.");
- DrawCenteredString(theStr, &theY, width);
- theStr[0]=0x00;
- DrawCenteredString(theStr, &theY, width);
- AddToString(theStr, "\p(But can you do it with only ");
- NumToString(best, numStr);
- AddToString(theStr, numStr);
- AddToString(theStr, "\p?)");
- DrawCenteredString(theStr, &theY, width);
- }
- }
-
- short BestSolution(short numRows, short numColumns)
- {
- switch (numRows)
- {
- case 5: return 5;
- case 6: return 7;
- case 7: return 7;
- case 8: return 9;
- }
- }
-
- short FindNumUsed(void)
- {
- short theRow, theColumn;
- Boolean used[11];
- short numUsed;
-
- for (theRow=0; theRow<11; theRow++)
- used[theRow]=FALSE;
-
- for (theRow=0; theRow<gNumRows; theRow++)
- {
- for (theColumn=0; theColumn<gNumColumns; theColumn++)
- {
- used[Board[theRow][theColumn]]=TRUE;
- }
- }
-
- numUsed=0;
- for (theRow=0; theRow<11; theRow++)
- if (used[theRow])
- numUsed++;
-
- return numUsed;
- }
-
- void DrawCenteredString(Str255 theStr, short *theY, short width)
- {
- TextFont(geneva);
- TextSize(9);
- TextMode(srcXor);
- MoveTo((width-StringWidth(theStr))/2, *theY);
- DrawString(theStr);
- (*theY)+=12;
- }
-
- void AddToString(Str255 theStr, Str255 addStr)
- {
- Mymemcpy((Ptr)&theStr[theStr[0]+1], (Ptr)&addStr[1], addStr[0]);
- theStr[0]+=addStr[0];
- }
-